---
title: "Subgroup analysis: age"
author: "[redacted]"
date: "2025-04-07"
output: html_document
---

```{r}
```

#####LOAD PACKAGES AND DATA

```{r}
#Load packages
library(tidyverse)
library(cjoint)
library(cregg)


#Load data
conjoint_age <- cjoint::read.qualtrics(
  "data_clean.csv",
  covariates = c("age"),
  respondentID = "ResponseId",
  letter = "F",
  new.format = TRUE,
  responses = paste0("conjoint_", c("intro", 1:8))
)

#Remove NAs
conjoint_age <- conjoint_age %>% drop_na(selected)

names(conjoint_age)

```

#RECODE COVARIATE

```{r}

conjoint_age$age <- as.numeric(as.character(conjoint_age$age))

conjoint_age$age_years = 2024 - conjoint_age$age

#AGE
median(conjoint_age$age_years, na.rm = TRUE)

conjoint_age <- conjoint_age %>%
  mutate(
    age_cat = ifelse(age_years >= 18 & age_years <= 54, "Younger",
              ifelse(age_years >= 55 & age_years <= 100, "Older", NA)),
    age_cat = factor(age_cat, levels=c("Younger", "Older"))  # Convert age_cat to a factor
  )

summary(conjoint_age$age_cat)

```

#RENAME VARIABLES

```{r}

# Renaming attributes and levels in English
conjoint_age <-  conjoint_age %>% dplyr::rename(Topic=Onderwerp, 
                                            Relation.to.you=Relatie.tot.u,
                                            Apparent.knowledge.of.the.person=Schijnbare.kennis.van.de.persoon,
                                            Apparent.concern.of.the.person=Schijnbare.bezorgdheid.van.de.persoon,
                                            Apparent.motivation.of.the.person=Schijnbare.motivatie.van.de.persoon,
                                            Closeness.with.you=Band.met.u,
                                            Gender.of.the.person=Geslacht.van.de.persoon
                                            ) %>%
                                     mutate(
                                            Topic = recode_factor(Topic,
                                                  "overstromingen" = "floods", 
                                                  "besmettelijke ziekten" = "infectious diseases",
                                                  "pesticiden" = "pesticides",
                                                  "levensstijlziekten" = "lifestyle diseases",),
                                            Gender.of.the.person = recode_factor(Gender.of.the.person,
                                                  "vrouw" = "female", 
                                                  "man" = "male"),
                                            Relation.to.you = recode_factor(Relation.to.you,
                                                  "vriend/vriendin (platonisch)" = "friend",
                                                  "collega/studiegenoot" = "colleague/fellow student",
                                                  "familielid" = "relative"),
                                            Closeness.with.you = recode_factor(Closeness.with.you,
                                                  "zeer hecht" = "very close", 
                                                  "redelijk hecht" = "fairly close",
                                                  "enigszins hecht" = "somewhat close",
                                                  "niet hecht" = "not close"),
                                            Apparent.knowledge.of.the.person = recode_factor(Apparent.knowledge.of.the.person,
                                                  "zeer goed geinformeerd" = "very well-informed", 
                                                  "redelijk goed geinformeerd" = "fairly well-informed",
                                                  "enigszins geinformeerd" = "somewhat well-informed",
                                                  "niet geinformeerd" = "not well-informed"),
                                            Apparent.concern.of.the.person = recode_factor(Apparent.concern.of.the.person,
                                                  "zeer bezorgd" = "very concerned", 
                                                  "redelijk bezorgd" = "fairly concerned",
                                                  "enigszins bezorgd" = "somewhat concerned",
                                                  "niet bezorgd" = "not concerned"),
                                            Apparent.motivation.of.the.person = recode_factor(Apparent.motivation.of.the.person,
                                                  "u te overtuigen om meer maatregelen te nemen" = "to convince you to take more precautions", 
                                                  "informatie over het risico met u te willen uitwisselen" = "to exchange information with you",
                                                  "zich minder ongerust voelen" = "to feel less anxious",
                                                  "u te overtuigen om minder maatregelen te nemen" = "to convince you to take fewer precautions"))


#Check renamings
table(conjoint_age$Topic)
table(conjoint_age$Gender.of.the.person)
table(conjoint_age$Relation.to.you)
table(conjoint_age$Closeness.with.you)
table(conjoint_age$Apparent.knowledge.of.the.person)
table(conjoint_age$Apparent.concern.of.the.person)
table(conjoint_age$Apparent.motivation.of.the.person)


```

#CHANGE VARIABLE ORDER AND SET REFERENCE CATEGORIES

```{r}

#REORDER LEVELS
conjoint_age$Topic <- factor(conjoint_age$Topic,
                                           levels = c("pesticides", "floods", "lifestyle diseases", "infectious diseases"))
conjoint_age$Gender.of.the.person <- factor(conjoint_age$Gender.of.the.person,
                                           levels = c(  "male", "female"))
conjoint_age$Relation.to.you <- factor(conjoint_age$Relation.to.you,
                                           levels = c("colleague/fellow student", "friend", "relative"))
conjoint_age$Closeness.with.you <- factor(conjoint_age$Closeness.with.you,
                                           levels = c("not close", "somewhat close", "fairly close", "very close"))
conjoint_age$Apparent.knowledge.of.the.person <- factor(conjoint_age$Apparent.knowledge.of.the.person,
                                           levels = c("not well-informed", "somewhat well-informed", "fairly well-informed", "very well-informed"))
conjoint_age$Apparent.concern.of.the.person <- factor(conjoint_age$Apparent.concern.of.the.person,
                                           levels = c("not concerned", "somewhat concerned", "fairly concerned", "very concerned"))
conjoint_age$Apparent.motivation.of.the.person <- factor(conjoint_age$Apparent.motivation.of.the.person,
                                           levels = c("to convince you to take more precautions", "to convince you to take fewer precautions", "to feel less anxious", "to exchange information with you"))


```


######RUN MAIN AMCE, MM, AND DIFFERENCE IN MARGINAL MEANS

```{r}
###AMCE

#Full model
amce_age <- cj(conjoint_age, selected ~ 
             Topic +
             Gender.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person +
             Apparent.concern.of.the.person +
             Apparent.motivation.of.the.person, 
                  by = ~ age_cat, 
                  id = ~ Response.ID,
                  estimate = "amce")


# Define the dodge width to create separation between age group lines
dodge_width <- 1

amce_age_plot <- ggplot(amce_age, aes(x = estimate, y = level, color = age_cat, shape = age_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("red", "darkred")) +  
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "AMCE", y = "All attributes", color = "Age", shape = "Age") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.3, 0.3)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/age/amce_age.png", plot = amce_age_plot, width = 8, height = 6, dpi = 300)


###MARGINAL MEANS

mm_age <- cj(conjoint_age, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ age_cat, 
                  id = ~ Response.ID,
                  estimate = "mm")


mm_age$feature <- factor(mm_age$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between younger and older lines
dodge_width <- 1

mm_age_plot <- ggplot(mm_age, aes(x = estimate, y = level, color = age_cat, shape = age_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("red", "darkred")) +  
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Marginal Means", y = "All attributes", color = "Age", shape = "Age") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(0.3, 0.7)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/age/mm_age.png", plot = mm_age_plot, width = 8, height = 6, dpi = 300)

###DIFFERENCE IN MARGINAL MEANS

diff_age <- cj(conjoint_age, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ age_cat, 
                  id = ~ Response.ID,
                  estimate = "mm_differences")


diff_age$feature <- factor(diff_age$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between younger and older lines
dodge_width <- 1

diff_age_plot <- ggplot(diff_age, aes(x = estimate, y = level, color = age_cat, shape = age_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("darkred")) +  
  scale_shape_manual(values = c(17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Difference in Marginal Means", y = "All attributes", color = "Age", shape = "Age") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.15, 0.15)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/age/diff_age.png", plot = diff_age_plot, width = 8, height = 6, dpi = 300)

```

#####DIAGNOSTICS

```{r}

# BALANCE TESTING

# Manually map age_cat to numeric (assuming "Younger" and "Older")
conjoint_age$age_cat_num <- ifelse(conjoint_age$age_cat == "Younger", 0, 
                                      ifelse(conjoint_age$age_cat == "Older", 1, NA))

# Check for any NA values introduced by this process
table(conjoint_age$age_cat_num)

# Calculate marginal means using your dataset and attributes
agebalance <- plot(mm(conjoint_age, age_cat_num ~ 
                 Topic +
                 Gender.of.the.person +
                 Apparent.motivation.of.the.person +
                 Apparent.concern.of.the.person +
                 Relation.to.you +
                 Closeness.with.you + 
                 Apparent.knowledge.of.the.person,
                 id = ~Response.ID), 
                xlim = c(0, 1), vline = mean(conjoint_age$age_cat_num, na.rm = TRUE))

# Adjust the plot for larger text and dots
agebalance_adj <- agebalance +
  geom_point(size = 3, position = position_dodge(width = 0.5)) +
    ggtitle("Balance Testing: Age") +  # Add the title here
    labs(color = "Attribute") +  # Change the label for the color legend
  theme(
    text = element_text(size = 20),            # Increase base text size
    axis.title = element_text(size = 22),      # Increase axis title size
    axis.text = element_text(size = 18),       # Increase axis tick labels size
    legend.title = element_text(size = 22),    # Increase legend title size
    legend.text = element_text(size = 18),     # Increase legend text size
    plot.title = element_text(size = 24, face = "bold"),  # Increase plot title size and style
    strip.text = element_text(size = 18)       # Increase facet labels text size (if using facets)
  ) +
  guides(color = guide_legend(nrow = 4))  # Adjust the number of rows in the legend

# Save the plot
ggsave("diagnostics/balance_age.png", plot = agebalance_adj, width = 15, height = 11, dpi = 300)

```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
